package day3;

import org.junit.Test;

public class ArrayTest {

	@Test
	public void test1() {
		int score1 = 34;
		int score2 = 84;
		int score3 = 89;
		//int score[] = new int[4];
		int[] array1 = {34, 56, 87};
		int[] array = new int[4];
		array[0] = 34;
		array[1] = 56;
		array[2] = 87;
		array[3] = 56;
		System.out.println(array.length);
		for (int i = 0; i < array.length; i++) {
			System.out.println(array[i]);
		}
		
		for (int i = array.length - 1; i >= 0; i--) {
			System.out.println(array[i]);
		}
	}
	
	@Test
	public void test2() {
		int[] array = new int[4];
		array[0] = 34;
		array[1] = 56;
		array[2] = 87;
		array[3] = 59;
		for (int i = 0; i <= array.length; i++) {
			System.out.println(array[i]);// array[4]
			// java.lang.ArrayIndexOutOfBoundsException: 4
		}
	}
	
	//3、int[] array = [23, 56, 8, 99, 2];
	@Test
	public void test11() {
		int[] array = {23, 56, 8, 99, 2};
		// 假设数组中第一个元素是最大值
		int max = array[0];
		for (int i = 1; i < array.length; i++) {
			// 如果这个成立，证明假设是错误的
			if (max < array[i]) {
				// 更新一下max存放的最大值
				max = array[i];
			}
		}
		
		System.out.println("max: " + max);
	}
	
	
	
	// 要求求出其中的奇数个数和偶数个数。
	// int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
	@Test
	public void test44() {
		int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
		int oddCount = 0;
		int evenCount = 0;
		for (int i = 0; i < scores.length; i++) {
			if (scores[i] % 2 == 0) {
				evenCount++;
			} else {
				oddCount++;
			}
		}
		
		System.out.println("奇数个数：" + oddCount);
		System.out.println("偶数个数：" + evenCount);
	}
	
	
	// 求出上面数组中0-9分别出现的次数（有一点难度）
	// int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
	@Test
	public void test22() {
		int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
		int[] counts = new int[10];
		for (int i = 0; i < scores.length; i++) {
			switch (scores[i]) {
			case 0:
				counts[0]++;
				break;
			case 1:
				counts[1]++;
				break;
			case 2:
				counts[2]++;
				break;
			case 3:
				counts[3]++;
				break;
			// .....
			case 9:
				counts[9]++;
				break;
			default:
				break;
			}
		}
		
		for (int i = 0; i < counts.length; i++) {
			System.out.println(i + "出现的次数" + counts[i]);
		}
	}
	
	@Test
	public void test23() {
		int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
		int[] count = new int[10];
		for (int i = 0; i < scores.length; i++) {
			switch (scores[i]) {
			case 0:
				count[0]++;
				break;
			case 1:
				count[1]++;
				break;
			case 2:
				count[2]++;
				break;
			case 3:
				count[3]++;
				break;
			case 4:
				count[4]++;
				break;
			case 5:
				count[5]++;
				break;
			case 6:
				count[6]++;
				break;
			case 7:
				count[7]++;
				break;
			case 8:
				count[8]++;
				break;
			case 9:
				count[9]++;
				break;

			default:
				break;
			}
		}
		for (int i = 0; i < count.length; i++) {
			System.out.println(i+"出现的次数"+count[i]);
		}
	}
	
	
	@Test
	public void test55() {
		// 在方法里面定义的变量都叫局部变量 local variable
		int temp = 2;
		for (int i = 0; i < 10; i++) {
			// Duplicate local variable i
			for (int j = 0; j < 34; j++) {
				
			}
		}
		
		for (int i = 0; i < 23; i++) {
			
		}
	}
	
	
	
	
}
